<template>
	<view class="querendingdan">
		<headertop title="确认订单" str1="oridei" :back1="back1"></headertop>
		<view class="quedingbox">

		</view>
		<view :style="{'margin-top':statusBarHeight+'px','width':'100%','height':barHeight+'px'}">

		</view>
		<view class="centerbox">
			<view class="dzbox">
				<view class="center">
					<template v-if="dzobj">
						<view class="dzline">
							<image :src="_static('/images/Frame@2x(45).png')" class="dzhiiocn" mode="">
							</image> <span style="margin-left:16rpx ;">{{dzobj.consignee}}</span> <span
								style="margin-left:24rpx ;">{{dzobj.mobile}}</span>
						</view>
					</template>
					<view class="dzline1" @click="godz" v-if="dzobj">
						<view class="dztext">
							{{dzobj.province_name}}{{dzobj.city_name}}{{dzobj.district_name}}{{dzobj.address}}
						</view>
						<image :src="_static('/images/Frame@2x(46).png')" class="arrowright" mode="">
						</image>
					</view>
					<view class="title" @click="godz" style="display:flex;align-items: center;  margin-top: 30rpx;"
						v-else>
						<image :src="_static('/images/Frame@2x(45).png')" class="dzhiiocn" style="margin-right:16rpx;"
							mode="">
						</image>
						添加地址
						<image :src="_static('/images/Frame@2x(46).png')" style="width: 36rpx;height: 36rpx;" mode="">
						</image>
					</view>

				</view>
			</view>
			<view class="spbox1">
				<view class="spbox" v-if="typestr!='购物车'">
					<image :src="_static('/images/image@2x(4).png')" class="leftimg" mode=""></image>
					<view class="texts">
						<view class="t1">
							{{shangpingobj['title']}}
						</view>

						<view class="t2">
							{{shangpingobj['subtitle']}}
						</view>
						<view class="t3line">
							<view class="box1">
								x1
							</view>
							<view class="gnq">
								<image :src="_static('/images/Group 11197@2x.png')" class="icon" mode=""
									@click="add('-')">
								</image>
								<span>{{selectedSkuPrice['goods_num']}}</span>
								<image :src="_static('/images/Group 11462@2x.png')" class="icon" mode=""
									@click="add('+')">
								</image>
							</view>
						</view>
					</view>
				</view>

				<view class="spbox" v-if="typestr=='购物车'" v-for="(item,index) in gouwc" :key="item.id">
					<image :src="item['sku_price']['image']||item['goods']['image']" class="leftimg" mode=""></image>
					<view class="texts">
						<view class="t1">
							{{item['goods']['title']}}
						</view>
						<view class="t2line">
							<view class="t2">
								{{item['goods']['subtitle']}}
							</view>
							<view class="box1">
								<span
									v-if="jineobj['goods_list'][index]['current_sku_price']['goods_sku_text'].length>1"><span
										style="font-size: 24rpx;color: #7F7F7F;">{{jineobj['goods_list'][index]['current_sku_price']['goods_sku_text'][0]}}
										|
										{{skufun(jineobj['goods_list'][index]['current_sku_price']['goods_sku_text'])[1]}}</span></span>
								<span v-else><span
										style="font-size: 24rpx;color: #7F7F7F;">{{jineobj['goods_list'][index]['current_sku_price']['goods_sku_text'][0]}}</span></span>
							</view>
						</view>
						<view class="t3line">
							<view class="box1">
								x1
							</view>
							<view class="gnq">
								<image :src="_static('/images/Group 11197@2x.png')" class="icon" mode=""
									@click="add('-',index)"></image>
								<span>{{item['goods_num']}}</span>
								<image :src="_static('/images/Group 11462@2x.png')" class="icon" mode=""
									@click="add('+',index)"></image>
							</view>
						</view>
						<view class="jiage1">
							￥<span>{{jigaefun(item.sku_price.price)}}.</span><span>{{jigaefun(item.sku_price.price,'后')}}</span>
						</view>
					</view>
				</view>


				<view class="bbtm">
					<view class="ll">
						<view class="bt">
							配送：
						</view>
						<view class="rightcontent">
							<span>快递运输</span>
							<image :src="_static('/images/Frame@2x(47).png')" mode="">
							</image>
						</view>
					</view>
					<view class="ll">
						<view class="bt">
							备注：
						</view>
						<view class="rightcontent">
							<input type="text" placeholder="请输入备注" />
						</view>
					</view>
				</view>
			</view>
			<view class="jigemx">
				<view class="center">
					<view class="t1">
						价格明细
					</view>
					<view class="line1">
						<view class="tt">
							商品总价：共{{jianshu}}件商品
						</view>
						<view class="tt">
							￥{{jineobj.goods_amount}}
						</view>
					</view>
					<view class="line1">
						<view class="tt">
							运费：
						</view>
						<view class="tt">
							￥{{jineobj.dispatch_amount}}
						</view>
					</view>
					<view class="bottomline">
						<view class="tt1">
							实付款：
						</view>
						<view class="jiage1">
							￥{{jineobj.order_amount }}
						</view>
					</view>
				</view>
			</view>
			<view class="zhifubox" @click="active2=1">
				<image :src="_static('/images/Group 11345@2x.png')" class="logoicon" mode=""></image>
				<view class="title">
					微信支付
				</view>
				<image :src="_static('/images/Group 11469@2x(1).png')" class="xq" mode=""></image>
			</view>
		</view>
		<view class="" style="height: 200rpx;">

		</view>
		<view class="fixed">
			<view class="center">
				<view class="hejitext">
					合计：<span style="color: #F53F3F;font-size:32rpx ;">￥{{jineobj.order_amount}}</span>
				</view>
				<image :src="_static('/images/Button@2x.png')" class="tijiaoorider" @click="tjdd" mode="">
				</image>
			</view>
		</view>
		<up-popup :show="show" mode="center">
			<view class="centerboxx">
				<view class="center1">
					<image :src="_static('/images/Frame@2x(50).png')" class="duih" mode=""></image>
					<view class="tt">
						支付成功
					</view>
					<view class="qdbtn" @click="gopage()">
						确定
					</view>
				</view>
			</view>
		</up-popup>
	</view>
</template>
<script setup>
	import headertop from '@/components/header.vue'
	import {
		_static
	} from '@/ulm/utils/url.js'
	import ulm, {
		$api
	} from '@/ulm';
	import {
		ref,
		onMounted,
		computed
	} from 'vue'
	import {
		onPageScroll,
		onLoad,
		onShow,
		onReachBottom
	} from "@dcloudio/uni-app";
	const statusBarHeight = ref('')
	const shangpingobj = ref({})
	const gouwc = ref({})
	const dzobj = ref({})
	const back1 = ref('')
	const jineobj = ref({})
	const selectedSkuPrice = ref({})
	const barHeight = ref('')
	const typestr = ref('')
	const show = ref(false)
	const payoid1 = ref('')
	const query = reactive({
		order_type: 'goods',
		goods_list: {},
		address_id: '',
		from: ''
	})
	const jianshu = computed(() => {
		return query.goods_list.reduce((e, item) => {
			return e + item.goods_num
		}, 0)
	})
	onShow(() => {
		const pages = getCurrentPages()
		const currentPage = pages[pages.length - 1]
		const options = currentPage.options
		typestr.value = options.typestr
		if (typestr.value == '购物车') {
			carinit()
			query['from'] = 'cart'
		} else {
			shangpingobj.value = uni.getStorageSync('detailobj')
			selectedSkuPrice.value = uni.getStorageSync('selectedSkuPrice')
			var shuju = [{
				'goods_id': shangpingobj.value.id,
				'goods_num': selectedSkuPrice.value.goods_num,
				'goods_sku_price_id': selectedSkuPrice.value.id
			}]
			query.goods_list = shuju
			getdz()
			query['from'] = ''
		}
	})

	function skufun(str) {
		if (!str) {
			return []
		}
		return str.split(',')
	}
	async function carinit(type) {
		const res = await $api.cartnum()
		if (res.code == 1) {
			var arr = uni.getStorageSync('gouwc')
			for (var i in res.data) {
				var ind = arr.findIndex(item => item.id == res.data[i]['id'])
				if (ind != -1) {
					arr[ind]['sku_price']['stock'] = res.data[i]['sku_price']['stock']
				}
			}
			gouwc.value = arr
			var shuju = []
			for (var i in gouwc.value) {
				var obj = {
					'goods_id': gouwc.value[i].goods_id,
					'goods_num': gouwc.value[i].goods_num,
					'goods_sku_price_id': gouwc.value[i]['sku_price']['id']
				}
				shuju.push(obj)
			}
			query.goods_list = shuju
			getdz()
		}
	}

	onPageScroll((e) => {
		if (e.scrollTop > 100) {
			back1.value = '#81D6FF'
		} else {
			back1.value = ''
		}
	})

	function jigaefun(str1, strr) {
		let number = Number(str1).toFixed(2)
		let str = number.toString(); // 转换为字符串 "100.00"
		let parts = str.split('.'); // 按小数点分割
		let integerPart = parts[0]; // "100"
		let decimalPart = parts[1] || "00"; // "00"（如果没有小数部分，默认 "00"）
		if (!strr) {
			return integerPart
		} else {
			return decimalPart
		}

	}

	async function getdz(id) {
		const res = await $api.mrdz()
		console.log(172172172172172, res, 'res');
		if (res.code == 1) {
			if (res.data) {
				dzobj.value = res.data
				query.address_id = dzobj.value.id

			} else {
				const res1 = await $api.addressIndex()
				if (res1.code == 1) {
					if (!res1.data.length) {
						dzobj.value = null
					} else {
						dzobj.value = res1.data[0]
						query.address_id = dzobj.value.id
					}
				}
			}
			jisuanjiage()
		}

	}

	async function tjdd() {
		if (!query.address_id) {
			uni.showToast({
				title: '请添加快递地址',
				icon: 'none'
			})
			return
		}
		const res = await $api.createorder(query)
		if (res.code == 1) {
			var payoid = res.data.id
			payoid1.value = payoid
			const res1 = await $api.payPrepay({
				order_sn: res.data.order_sn,
				payment: 'wechat'
			})
			if (res1.code == 1) {
				var payobj = res1.data.pay_data
				uni.requestPayment({
					timeStamp: String(payobj.timeStamp),
					nonceStr: payobj.nonceStr,
					package: payobj.package,
					signType: payobj.signType,
					paySign: payobj.paySign,
					success(res) {
						show.value = true
					},
					fail(e) {
						setTimeout(() => {
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							})
						}, 500)
						uni.redirectTo({
							url: '/sub_shoppingmal/index/orderdetail?id=' + payoid
						})
					}
				})
			}
		}

	}

	function gopage() {
		show.value = false
		uni.redirectTo({
			url: '/sub_shoppingmal/index/orderdetail?id=' + payoid1.value
		})
	}

	function godz() {
		uni.navigateTo({
			url: '/sub_my/my/shdz?typestr=dz'
		})
	}

	function add(type, ind) {
		if (typestr.value == '购物车') {
			if (type == '+') {
				if (gouwc.value[ind]['goods_num'] >= gouwc.value[ind]['sku_price']['stock']) {
					uni.showToast({
						title: '商品库存不足',
						icon: 'none'
					})
					return
				}
				gouwc.value[ind]['goods_num']++
			} else {
				if (gouwc.value[ind]['goods_num'] == 1) return
				gouwc.value[ind]['goods_num']--
			}
			var shuju = []
			for (var i in gouwc.value) {
				var obj = {
					'goods_id': gouwc.value[i].goods_id,
					'goods_num': gouwc.value[i].goods_num,
					'goods_sku_price_id': gouwc.value[i]['sku_price']['id']
				}
				shuju.push(obj)
			}
			query.goods_list = shuju
		} else {
			if (type == '+') {
				if (selectedSkuPrice.value['goods_num'] >= selectedSkuPrice.value['stock']) {
					uni.showToast({
						title: '商品库存不足',
						icon: 'none'
					})
					return
				}
				selectedSkuPrice.value['goods_num']++
			} else {
				if (selectedSkuPrice.value['goods_num'] == 1) return
				selectedSkuPrice.value['goods_num']--
			}
			var shuju = [{
				'goods_id': shangpingobj.value.id,
				'goods_num': selectedSkuPrice.value.goods_num,
				'goods_sku_price_id': selectedSkuPrice.value.id
			}]
			query.goods_list = shuju
		}

		jisuanjiage()
	}

	async function jisuanjiage() {
		const res = await $api.calcorder(query)
		if (res.code == 1 || res.code == 0) {
			jineobj.value = res.data
		}
	}

	onMounted(() => {
		statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight
		const {
			top,
			height
		} = wx.getMenuButtonBoundingClientRect();
		barHeight.value = height ? height + (top - statusBarHeight.value) * 2 : 38;
	})
</script>

<style lang="scss">
	.jiage1 {
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		font-size: 32rpx;
		color: #F53F3F;
	}

	::v-deep .u-popup__content {
		border-radius: 32rpx 32rpx 32rpx 32rpx;
	}

	.centerboxx {
		width: 518rpx;
		height: 488rpx;
		background: linear-gradient(180deg, #CAEEFF 0%, #FFFFFF 23%);
		border-radius: 32rpx 32rpx 32rpx 32rpx;
		overflow: hidden;
		position: relative;

		.closeicon {
			width: 44rpx;
			height: 44rpx;
			position: absolute;
			right: 24rpx;
			top: 24rpx;
		}

		.center1 {
			display: flex;
			flex-direction: column;
			align-items: center;

			.duih {
				width: 124rpx;
				height: 124rpx;
				margin: auto;
				margin-top: 90rpx;
			}

			.tt {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				margin-top: 30rpx;
				color: #1D2129;
			}

			.qdbtn {
				width: 244rpx;
				height: 74rpx;
				background: #2AB4F5;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				line-height: 74rpx;
				text-align: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 28rpx;
				color: #FFFFFF;
				margin-top: 60rpx;
			}
		}

	}

	page {
		background: #F7F8FA;
	}

	.fixed {
		width: 100%;
		position: fixed;
		bottom: 0%;
		background: #FFFFFF;
		box-shadow: 0rpx -8rpx 16rpx 0rpx rgba(0, 0, 0, 0.08);
		overflow: hidden;
		height: 188rpx;

		.center {
			width: 686rpx;
			margin: auto;
			margin-top: 22rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.hejitext {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #4E5969;
			}

			.tijiaoorider {
				width: 420rpx;
				height: 88rpx;
			}
		}
	}

	.querendingdan {
		width: 100%;
		overflow: hidden;
		position: relative;

		.quedingbox {
			position: absolute;
			z-index: -1;
			width: 100%;
			height: 314rpx;
			background: linear-gradient(180deg, #CAEEFF 71%, rgba(240, 250, 255, 0) 100%);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
		}

		.centerbox {
			width: 686rpx;
			// overflow: hidden;
			margin-top: 18rpx;
			margin: auto;

			.zhifubox {
				width: 686rpx;
				height: 92rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.25);
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin: 32rpx auto;
				display: flex;
				align-items: center;
				position: relative;


				.logoicon {
					width: 52rpx;
					height: 52rpx;
					margin-left: 20rpx;
				}

				.title {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #1D2129;
					margin-left: 12rpx;
				}

				.xq {
					width: 32rpx;
					height: 32rpx;
					position: absolute;
					right: 0%;
					margin-right: 28rpx;
				}
			}

			.jigemx {
				width: 686rpx;
				height: 326rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.25);
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin: 28rpx auto;
				overflow: hidden;

				.center {
					width: 630rpx;
					margin: auto;
					margin-top: 32rpx;

					.t1 {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 28rpx;
						color: #1D2129;
						margin-bottom: 20rpx;
					}

					.line1 {
						width: 100%;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.tt {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 28rpx;
							color: #4E5969;
							margin-bottom: 24rpx;
						}


					}

					.bottomline {
						width: 100%;
						height: 102rpx;
						display: flex;
						align-items: center;
						border-top: 2rpx solid #F2F3F5;
						justify-content: space-between;

						.tt1 {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 28rpx;
							color: #4E5969;
						}

						.jiage1 {
							font-family: PingFang SC, PingFang SC;
							font-weight: bold;
							font-size: 32rpx;
							color: #F53F3F;
						}
					}
				}
			}

			.spbox1 {
				width: 100%;
				overflow: hidden;
				box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.25);
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				background: #FFFFFF;
				margin-top: 28rpx;

				.bbtm {
					width: 630rpx;
					height: 154rpx;
					margin: auto;
					border-top: 2rpx solid #F2F3F5;

					.ll {
						width: 630rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-top: 28rpx;

						.bt {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 28rpx;
							color: #4E5969;
						}

						.rightcontent {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 28rpx;
							color: #4E5969;
							display: flex;
							align-items: center;

							input {
								text-align: right;
							}

							image {
								width: 28rpx;
								height: 28rpx;
							}
						}
					}
				}

				.spbox {
					width: 686rpx;
					height: 212rpx;
					margin: auto;
					display: flex;
					align-items: center;

					.leftimg {
						width: 156rpx;
						height: 156rpx;
						border-radius: 16rpx 16rpx 16rpx 16rpx;
						margin-left: 28rpx;
					}

					.texts {
						width: 462rpx;
						margin-left: 22rpx;

						.t1 {
							width: 356rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 28rpx;
							color: #1D2129;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1;
							/* 限制文本显示为2行 */
							overflow: hidden;
						}

						.t2line {
							width: 100%;
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-top: 10rpx;

							.t2 {
								width: 356rpx;
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #7F7F7F;
								display: -webkit-box;
								-webkit-box-orient: vertical;
								-webkit-line-clamp: 1;
								/* 限制文本显示为2行 */
								overflow: hidden;
							}

							.box1 {
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #7F7F7F;
							}
						}


						.t3line {
							width: 100%;
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-top: 22rpx;

							.gnq {
								width: 148rpx;
								display: flex;
								justify-content: space-between;
								align-items: center;
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 32rpx;
								color: #1D2129;

								.icon {
									width: 48rpx;
									height: 48rpx;
								}
							}

							.box1 {
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #7F7F7F;
							}
						}
					}
				}
			}

			.dzbox {
				width: 100%;
				box-shadow: 0rpx 4rpx 14rpx 0rpx rgba(0, 0, 0, 0.09);
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				overflow: hidden;
				background: #FFFFFF;

				.center {
					width: 630rpx;
					margin: auto;

					.title {
						font-family: PingFang SC, PingFang SC;
						font-weight: 600;
						font-size: 32rpx;
						color: #333333;
						margin-bottom: 30rpx;
						margin-left: 25rpx;

						.dzhiiocn {
							width: 32rpx;
							height: 32rpx;
						}
					}

					.dzline1 {
						width: 100%;
						display: flex;
						justify-content: space-between;
						margin-top: 24rpx;
						margin-bottom: 34rpx;

						.dztext {
							width: 558rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #777777;
						}

						.arrowright {
							width: 32rpx;
							height: 32rpx;
						}
					}

					.dzline {
						width: 100%;
						margin-top: 34rpx;
						display: flex;
						align-items: center;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 32rpx;
						color: #333333;

						.dzhiiocn {
							width: 32rpx;
							height: 32rpx;
						}
					}
				}
			}
		}
	}
</style>